package com.chen.stopwatch.History;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.chen.stopwatch.lap_view.LapItem;

public class HistoryDB {
	private SQLiteDatabase mSqLiteDatabase = null;

	private static final String DB_PATH = "data/data/com.chen.stopwatch/";

	private static final String DB_NAME = "stopwatch.db";

	private static final String HISTORY_TABLE = "stopwatch_history";

	private static final String AUTO_ID = "id";

	private static final String LAP_TIME = "lap_time";

	private static final String TOTAL_TIME = "total_time";

	private static final String COLOR = "color";

	private static final String INSERT_TIME = "insert_time";

	public HistoryDB() {
		initDataBase();
	}

	private void initDataBase() {
		try {
			mSqLiteDatabase = SQLiteDatabase.openDatabase(
				DB_PATH + DB_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY);
			initTable();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private boolean initTable() {
		if (null == mSqLiteDatabase)
			return false;

		String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + HISTORY_TABLE
				+ "( " + AUTO_ID + " INTEGER PRIMARY KEY, " + LAP_TIME
				+ " INTEGER, " + TOTAL_TIME + " INTEGER, " + COLOR
				+ " INTEGER," + INSERT_TIME + " INTEGER)";
		mSqLiteDatabase.execSQL(CREATE_TABLE);

		return true;
	}

	public void insert(LapItem item) {
		try {
			if (null == mSqLiteDatabase || null == item)
				return;
			ContentValues values = new ContentValues();
			values.put(LAP_TIME, item.lapTime);
			values.put(TOTAL_TIME, item.totalTime);
			values.put(COLOR, item.color);
			values.put(INSERT_TIME, item.insertTime);
			mSqLiteDatabase.insert(HISTORY_TABLE, null, values);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updateColor(LapItem item) {
		try {
			if (null == mSqLiteDatabase || null == item)
				return;

			ContentValues values = new ContentValues();
			String where = INSERT_TIME + "=? and " + TOTAL_TIME + "=? and "
					+ LAP_TIME + "=?";
			String[] whereValues = { Long.toString(item.insertTime),
					Long.toString(item.totalTime), Long.toString(item.lapTime) };
			values.put(COLOR, item.color);
			int number = mSqLiteDatabase.update(
				HISTORY_TABLE, values, where, whereValues);
			// Utils.logDebug("test", "updateColor = "
			// + ((number == 1) ? "true" : "false"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Cursor query() {
		try {
			return mSqLiteDatabase.query(
				HISTORY_TABLE, null, null, null, null, null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void delete(long insertTime) {
		try {
			String whereClause = INSERT_TIME + "= ?";
			String[] whereArgs = { Long.toString(insertTime) };
			mSqLiteDatabase.delete(HISTORY_TABLE, whereClause, whereArgs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static LapItem getDataByCursor(Cursor cursor) {
		if (null == cursor)
			return null;
		LapItem item = new LapItem();
		item.id = cursor.getInt(cursor.getColumnIndex(AUTO_ID));
		item.lapTime = cursor.getLong(cursor.getColumnIndex(LAP_TIME));
		item.totalTime = cursor.getLong(cursor.getColumnIndex(TOTAL_TIME));
		item.color = cursor.getInt(cursor.getColumnIndex(COLOR));
		item.insertTime = cursor.getLong(cursor.getColumnIndex(INSERT_TIME));
		return item;
	}

	public void close() {
		if (null != mSqLiteDatabase) {
			mSqLiteDatabase.close();
			mSqLiteDatabase = null;
		}
	}

}
